<?php
class Route{
	protected $_name;
	protected $_pattern;
	protected $_parram;
	const ROUTE_MATCH = 1;
	const ROUTE_NOTMATCH = 0;
	const ROUTE_ISMATCH = 'isMatch';
	const ROUTE_URL = 'url';
	const ROUTE_QUERY = 'query_string';
	/**
	 * Enter description here ...
	 * @param String $name
	 * @param String $pattern Regular Expression
	 * @param array $parram
	 */
	public function __construct($name,$pattern,$parram){
		$this->_name = $name;
		$this->_pattern = $pattern;
		$this->_parram = $parram;
	}
	public function match($strMap){
		$result = array();
		$result[Route::ROUTE_ISMATCH] = Route::ROUTE_NOTMATCH;
		$result[Route::ROUTE_URL] = preg_replace($this->_pattern, $this->parramToString(), $strMap);
		if(strcmp($strMap, $result[Route::ROUTE_URL]) != 0) $result[Route::ROUTE_ISMATCH] = Route::ROUTE_MATCH;
		return $result;
	}
	private function parramToString(){
		$str = "";
		$i = true;
		foreach ($this->_parram as $key=>$value) {
			if($i){
				$str .= $key . "=" . $value;
				$i = false;
			}
			else {
				$str .= "&" . $key . "=" . $value;
			}
		}
		return $str;
	}
	
	public function getName(){
		return $this->_name;
	}
	public function getParram(){
		return $this->_parram;
	}
	public function getPattern(){
		return $this->_pattern;
	}
}